#include "vltoolbar.h"

VLToolBar::VLToolBar(QWidget *parent) :
    QToolBar(tr("Main Toolbar"), parent)
{
    brushBtn = createAndConnect(tr(".\\icons\\256\\Brush.png"), SLOT(setBrush()));
    fillBtn = createAndConnect(tr(".\\icons\\256\\Paint.png"), SLOT(setFill()));
    selectBtn = createAndConnect(tr(".\\icons\\256\\Stylus.png"), SLOT(setSelect()));
    eraserBtn = createAndConnect(tr(".\\icons\\256\\Eraser.png"), SLOT(setEraser()));
    transformBtn = createAndConnect(tr(".\\icons\\256\\3D.png"), SLOT(setTransform()));
    magicWandBtn = createAndConnect(tr(".\\icons\\256\\Magic Wand.png"), SLOT(setMagicWand()));
    gameElementsBtn = createAndConnect(tr(".\\icons\\256\\Pixel Art.png"), SLOT(setGameElements()));
    terrainBtn = createAndConnect(tr(".\\icons\\256\\Bezier Curve.png"), SLOT(setTerrain()));
    currentColor = getColorFromGlobalState();
    colorBtn = createAndConnect(currentColor, SLOT(setColor()));

    setDefaults();
    // NOTE: [widget]->setFocusPolicy() to modify keyboard focus
}

void VLToolBar::setDefaults() {
    currentBtn = brushBtn;
    currentBtn->setChecked(true);
    setBrush();
}

void VLToolBar::setBrush() {
    if (currentBtn != brushBtn) {
        setBtn(brushBtn);
        emit brushClicked();
    }
    currentBtn->setChecked(true);
}

void VLToolBar::setFill() {
    if (currentBtn != fillBtn) {
        setBtn(fillBtn);
        emit fillClicked();
    }
    currentBtn->setChecked(true);
}

void VLToolBar::setSelect() {
    if (currentBtn != selectBtn) {
        setBtn(selectBtn);
        emit selectClicked();
    }
    currentBtn->setChecked(true);
}

void VLToolBar::setEraser() {
    if (currentBtn != eraserBtn) {
        setBtn(eraserBtn);
        emit eraserClicked();
    }
    currentBtn->setChecked(true);
}

void VLToolBar::setTransform() {
    if (currentBtn != transformBtn) {
        setBtn(transformBtn);
        emit transformClicked();
    }
    currentBtn->setChecked(true);
}


void VLToolBar::setMagicWand() {
    if (currentBtn != magicWandBtn) {
        setBtn(magicWandBtn);
        emit magicWandClicked();
    }
    currentBtn->setChecked(true);
}

void VLToolBar::setGameElements() {
    if (currentBtn != gameElementsBtn) {
        setBtn(gameElementsBtn);
        emit gameElementsClicked();
    }
    currentBtn->setChecked(true);
}

void VLToolBar::setTerrain() {
    if (currentBtn != terrainBtn) {
        setBtn(terrainBtn);
        emit terrainClicked();
    }
    currentBtn->setChecked(true);
}

void VLToolBar::setColor() {
    emit colorClicked();
}

void VLToolBar::experimentalSetTool(QWidget *btn) {
    currentBtn->setChecked(false);
    currentBtn = (QToolButton *) btn;
    currentBtn->setChecked(true);

    emit toolClicked(btn);
}

void VLToolBar::changeColor(QColor color) {
    if (color != currentColor) {
        currentColor = color;
        colorMap->fill(color);
        colorBtn->setIcon(QIcon(*colorMap));
        //delete orig;
    }

    emit colorChanged(color);
}

QToolButton* VLToolBar::createAndConnect(QString icon, const char *member) {
    QToolButton *btn = new QToolButton();
    btn->setIcon(QIcon(icon));
    btn->setCheckable(true);
    this->addWidget(btn);
    connect(btn, SIGNAL(clicked()), this, member);

    return btn;
}

QToolButton* VLToolBar::createAndConnect(QColor color, const char *member) {
    QToolButton *btn = new QToolButton();
    colorMap = new QPixmap(256, 256);
    colorMap->fill(color);
    btn->setIcon(QIcon(*colorMap));
    this->addWidget(btn);
    connect(btn, SIGNAL(clicked()), this, member);

    return btn;
}

void VLToolBar::experimentalConnect() {
    sigMap = new QSignalMapper(this);
    sigMap->setMapping(brushBtn, brushBtn);
    connect(brushBtn, SIGNAL(clicked()), sigMap, SLOT(map()));
    connect(sigMap, SIGNAL(mapped(QWidget *)), this, SLOT(experimentalSetTool(QWidget *)));
}

QColor VLToolBar::getColorFromGlobalState() {
    GlobalStates *globalState;
    colour rgb;

    globalState = GlobalStates::getInstance();
    rgb = globalState->getRGB();
    return QColor::fromRgb(rgb.r, rgb.g, rgb.b);
}

void VLToolBar::setBtn(QToolButton *btn) {
    currentBtn->setChecked(false);
    currentBtn = btn;
}
